package com.cs2340.practice;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener; 
import android.widget.CompoundButton; 

public class TaskActivity extends Activity {
	private static final String TAG = "TaskActivity";
	private String user;
	private String curtask;
	private TaskDataBaseAdapter taskDB;
	private TextView task;
	private TextView taskDesc;
	private TextView taskDueDate;
	private TextView taskDueTime;
	private TextView taskLoc;
	private TextView taskCate;
	private CheckBox taskComp;
	private ListView taskView;
	private TaskListAdapter tla;
	private ArrayList<String> taskList;
	private ArrayList<String> detailList;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tasklistview);
		// after initializing vars

		Bundle gotBasket = getIntent().getExtras();
		user = gotBasket.getString("userKey");
		showUserTaskList(user);

	}

	/**
	 * Displays the list of task for the current user
	 * 
	 * @param listUser
	 *            the logged in user
	 */
	private void showUserTaskList(String listUser) {
		// TODO Auto-generated method stub
		setContentView(R.layout.tasklistview);
		taskDB = new TaskDataBaseAdapter(this);
		taskDB.openToWrite();
		//taskDB.insert("first sample", "this the first  sample task ", "never due","05:00", "my house", "work", listUser);
		//taskDB.insert("second sample", "this is a sample task ", "always due","09:17", "your house", "school", listUser);

		// get list of task names
		taskList = taskDB.getTaskNames(listUser);
		
		// if there are no tasks, make NoTask text box visible, else make it invisible
		TextView noTasks;
		noTasks = (TextView)findViewById(R.id.taskListTitle);
		if (taskList.isEmpty()) {
			noTasks.setText(R.string.tasklistnotasks);
		} else {
			noTasks.setText(R.string.Task_List);
		}
		taskDB.close();
		tla = new TaskListAdapter(taskList, this);
		taskView = (ListView) findViewById(R.id.list);
		taskView.setAdapter(tla);

		taskView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Log.i(TAG, "clicked: " + taskList.get(arg2).toString());

				showTaskDetails(taskList.get(arg2).toString());

			}
		});

		

	}
	
	/**
	 * shows only completed tasks in the tasklist
	 * @param listUser
	 */
	private void showCompletedTasks(String listUser) {
		
		setContentView(R.layout.tasklistview);
		taskDB = new TaskDataBaseAdapter(this);
		taskDB.openToWrite();

		// get list of task names
		taskList = taskDB.getCompletedTasks(listUser);
		// if there are no tasks, make NoTask text box visible, else make it invisible
		TextView noTasks;
		noTasks = (TextView)findViewById(R.id.taskListTitle);
		if (taskList.isEmpty()) {
			noTasks.setText(R.string.tasklistnotasks);
		} else {
			noTasks.setText(R.string.Task_List);
		}
		taskDB.close();
		tla = new TaskListAdapter(taskList, this);
		taskView = (ListView) findViewById(R.id.list);
		taskView.setAdapter(tla);
		
		taskView.setOnItemClickListener(new OnItemClickListener() {
		  
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Log.i(TAG, "clicked: " + taskList.get(arg2).toString());

				showTaskDetails(taskList.get(arg2).toString());
				
			}
		});	

	}

	/** onClick for going to the task creation screen. */
	public void gotoNewTask(View view) {
		setContentView(R.layout.newtask);
	}
	
	/** onClick for showing completed tasks */
	public void viewCompleted(View view) {
		setContentView(R.layout.tasklistview);
		showCompletedTasks(user);
	}
	
	/** onClick for showing uncompleted tasks */
	public void viewUnCompleted(View view) {
		setContentView(R.layout.tasklistview);
		showUnCompletedTasks(user);
	}
	
	public void viewByCategory(View view)
	{
		EditText theCategory;
			
		theCategory = (EditText) findViewById(R.id.categorySpinner);

		String category = theCategory.getText().toString();
		
		Log.i(TAG, "searching for: " + category );
		showUserTaskList(user);
		sortByCategory(user, category);
	}
	
	public void viewByDate(View view)
	{
		
		EditText theDate;
			
		theDate = (EditText) findViewById(R.id.sortByDateTextbox);

		String date = theDate.getText().toString();
		
		Log.i(TAG, "searching for: " + date );
		showUserTaskList(user);
		sortByDate(user, date);
	}
	
	public void viewLocation(View view) {
		
	    Intent intent = new Intent(TaskActivity.this, Map.class);
	    startActivity(intent);
	}
	
	private void showUnCompletedTasks(String listUser) {
		setContentView(R.layout.tasklistview);
		taskDB = new TaskDataBaseAdapter(this);
		taskDB.openToWrite();

		// get list of task names
		taskList = taskDB.getUnCompletedTasks(listUser);
		// if there are no tasks, make NoTask text box visible, else make it invisible
		TextView noTasks;
		noTasks = (TextView)findViewById(R.id.taskListTitle);
		if (taskList.isEmpty()) {
			noTasks.setText(R.string.tasklistnotasks);
		} else {
			noTasks.setText(R.string.Task_List);
		}
		taskDB.close();
		tla = new TaskListAdapter(taskList, this);
		taskView = (ListView) findViewById(R.id.list);
		taskView.setAdapter(tla);
		
		taskView.setOnItemClickListener(new OnItemClickListener() {
		  
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Log.i(TAG, "clicked: " + taskList.get(arg2).toString());

				showTaskDetails(taskList.get(arg2).toString());
				
			}
		});

		
	}

	/** onClick for going to the log in creation screen. */
	public void logOutUser(View view) {
		Intent openLoginActivity = new Intent(view.getContext(), LoginActivity.class);
		startActivity(openLoginActivity);
		//setContentView(R.layout.main);
	}
	


	/**
	 * Displays the task details for the selected task 
	 * 			-this is used mostly in taskdetailsview?
	 * 
	 * @param taskName
	 *            task who's detail will be displayed
	 */
	public void showTaskDetails(String task_name) {
		
		curtask =task_name;
		setContentView(R.layout.taskdetailsview);
		// get views
		task = (TextView) findViewById(R.id.taskName);
		taskDesc = (TextView) findViewById(R.id.description);
		taskDueDate = (TextView) findViewById(R.id.dueDate);
		taskDueTime = (TextView) findViewById(R.id.dueTime);
		taskLoc = (TextView) findViewById(R.id.location);
		taskCate = (TextView) findViewById(R.id.category);
		taskComp = (CheckBox) findViewById(R.id.completeCB); 
		taskDB = new TaskDataBaseAdapter(this);
		taskDB.openToWrite();
		
		detailList = taskDB.getTaskDetails(task_name);
		taskDB.close();
		
		if(detailList != null){
			// assign details to the text veiw
			String desc = detailList.get(0);//description
			String dueDate = detailList.get(1);//duedate
			String dueTime = detailList.get(2);//time
			String loc = detailList.get(3);//location
			String cate = detailList.get(4); //category
			String comp = detailList.get(5);
			// display data
			task.setText(task_name);
			taskDesc.setText(desc);
			taskDueDate.setText(dueDate);
			taskDueTime.setText(dueTime);
			taskLoc.setText(loc);
			taskCate.setText(cate);
			
			final String changed = task_name;
			
			CheckBox cb = (taskComp);

			if( Integer.parseInt(comp.toString()) == 1) cb.setChecked(true);
			else cb.setChecked(false);
			

			/**
			 * Changes value of completed to 0 or 1 (opposite of current) in the database
			 */
			cb.setOnCheckedChangeListener(new OnCheckedChangeListener()
				{
					@Override
				    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
				    {
							changeCompleteness(changed);
				    }
				});
			
			
		}

	}
	
	public void changeCompleteness(String task_name){
		taskDB.openToWrite();
		taskDB.switchCompleteness(task_name, user);
		taskDB.close();
	}
	

	/* Adds information from a new task to the database */
	public void addTask(View view) {
		taskDB = new TaskDataBaseAdapter(this);
		taskDB.openToWrite();

		EditText tname, tdes, tloc, ttime, tdate, tcat;

		tname = (EditText) findViewById(R.id.editTaskName);
		tdes = (EditText) findViewById(R.id.editTaskDescription);
		tloc = (EditText) findViewById(R.id.pickTaskLocation);
		ttime = (EditText) findViewById(R.id.timeInput);
		tdate = (EditText) findViewById(R.id.dateInput);
		tcat = (EditText) findViewById(R.id.editCategory);

		String tnamestr = tname.getText().toString();
		String tdesstr = tdes.getText().toString();
		String tlocstr = tloc.getText().toString();
		String ttimestr = ttime.getText().toString();
		String tdatestr = tdate.getText().toString();
		String tcatstr = tcat.getText().toString();
		


		taskDB.insert(tnamestr, tdesstr, tdatestr, ttimestr, tlocstr, tcatstr, user);

		taskDB.close();
		showUserTaskList(user);
	}

	public void gotoList(View view) {
		showUserTaskList(user);
	}
	
	

	
	/**
	 * brings screen to SortBy screen before returning to takslistview
	 */
	public void gotoSortBy(View view){
		setContentView(R.layout.sortby);	
	}
	
	
	private void sortByCategory(String listUser, String category_name){
		setContentView(R.layout.tasklistview);
		taskDB = new TaskDataBaseAdapter(this);
		taskDB.openToWrite();

		// get list of task names
		taskList = taskDB.getTasksByCategory(listUser, category_name);
		// if there are no tasks, make NoTask text box visible, else make it invisible
				TextView noTasks;
				noTasks = (TextView)findViewById(R.id.taskListTitle);
				if (taskList.isEmpty()) {
					noTasks.setText(R.string.tasklistnotasks);
				} else {
					noTasks.setText(R.string.Task_List);
				}
		taskDB.close();
		tla = new TaskListAdapter(taskList, this);
		taskView = (ListView) findViewById(R.id.list);
		taskView.setAdapter(tla);
		
		taskView.setOnItemClickListener(new OnItemClickListener() {
		  
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Log.i(TAG, "clicked: " + taskList.get(arg2).toString());

				showTaskDetails(taskList.get(arg2).toString());
			}
		});	
	}
	
	private void sortByDate(String listUser, String date){
		setContentView(R.layout.tasklistview);
		taskDB = new TaskDataBaseAdapter(this);
		taskDB.openToWrite();

		// get list of task names
		taskList = taskDB.getTasksByDate(listUser, date);
		// if there are no tasks, make NoTask text box visible, else make it invisible
		TextView noTasks;
		noTasks = (TextView)findViewById(R.id.taskListTitle);
		if (taskList.isEmpty()) {
			noTasks.setText(R.string.tasklistnotasks);
		} else {
			noTasks.setText(R.string.Task_List);
		}
		taskDB.close();
		tla = new TaskListAdapter(taskList, this);
		taskView = (ListView) findViewById(R.id.list);
		taskView.setAdapter(tla);
		
		taskView.setOnItemClickListener(new OnItemClickListener() {
		  
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Log.i(TAG, "clicked: " + taskList.get(arg2).toString());

				showTaskDetails(taskList.get(arg2).toString());
			}
		});	
	}
	
	/**
	 * onclick for going to the location picker
	 */
	public String getCurTask(){
		return curtask;	
	}
			

}